////////////////////////////////////////////////////////////////
///
/// File: SimpleTmpExp.m
/// Author: Lucio Davide Spano
/// Last update: 17 August 2012
/// Description: This class represent a generic ground term.
///              It is possible to define a concrete one
///              implementing a delegate class (ExpContent)
/// To do:
/// ------------------------------------------------------------
/// Copyright (c) 2012 Lucio Davide Spano
///                     (lucio.davide.spano@isti.cnr.it)
///
/// The use and distribution terms for this software are
/// contained in the file named license.txt, which can be found
/// in the root of this distribution.
/// By using this software in any fashion, you are agreeing to
/// be bound by the terms of this license.
///
/// You must not remove this notice, or any other, from this
/// software.
/// ------------------------------------------------------------
////////////////////////////////////////////////////////////////

#import "SimpleTmpExp.h"
#import "TmpExp.h"
#import "TmpExpProtected.h"
#import "ExpContent.h"

@implementation SimpleTmpExp

@synthesize content;
@synthesize token;

- (id) init{
    self = [super init];
    if(self){
        self.type = Simple;
    }
    return self;
}

- (id) initWithContent:(ExpContent *)c{
    self = [super init];
    if(self){
        self.type = Simple;
        self.content = c;
    }
    
    return self;
}

- (bool) accepts:(ExpEventArgs*) args{
    if(self.state != Enabled){
        return false;
    }
    if(self.content != nil){
        return [self.content accepts:args : self.token];
    }
    return false;
}

- (bool) consume:(ExpEventArgs *)args{
    if(self.content != nil){
        bool toRet = [self.content consume:args : self.token];
        if(toRet){
            [self setExecState:Completed :self.token];
            [self raiseCompleted:self :args :self.token];
        }else{
            [self setExecState:Error :self.token];
            [self raiseError:self :args :self.token];
        }
        [self raiseOnStateChanged:self :args :self.token];
        
        if(self.iterative){
            [self setExecState:Enabled :self.token];
        }
        
        return toRet;
        
    }
    
    return false;
}

- (void) invalidate:(ExpEventArgs *)args{
    [self setExecState:Error :self.token];
    [self raiseOnStateChanged:self :args :self.token];
    [self raiseError:self :args :self.token];
    
    if(self.iterative){
        [self setExecState:Enabled :self.token];
    }
}

- (void) setExecState:(int)_state :(Token *)_token{
    [super setExecState:_state :_token];
    switch(self.state){
        case Completed:
        case Enabled:
        case Performing:
            self.token = _token;
            break;
        default:
            break;
    }
}

- (void) notAccepted:(ExpEventArgs *)args{
    [self raiseOnNotAcceptedToken:self :args :token];
}

- (void) resetAccepted{
    
}

@end
